package com.xmy.solution.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xmy.solution.domain.UserSolutionFavorites;
import com.xmy.solution.domain.dto.solution.SolutionAppResDTO;
import com.xmy.solution.domain.dto.solution.SolutionReqDTO;
import com.xmy.solution.domain.dto.userFavorites.UserFavoritesDTO;
import com.xmy.solution.mapper.CategoriesMapper;
import com.xmy.solution.mapper.UserSolutionFavoriteMapper;
import com.xmy.solution.service.IUserSolutionFavoriteService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IUserSolutionFavoriteServiceImpl implements IUserSolutionFavoriteService {
    @Autowired
    private UserSolutionFavoriteMapper userSolutionFavoriteMapper;
    @Autowired
    private CategoriesMapper categoryMapper;

    @Override
    public int setFavorite(UserFavoritesDTO userSolution) {
        UserSolutionFavorites userSolutionFavorites = BeanUtil.toBean(userSolution, UserSolutionFavorites.class);
        if (userSolution.getIsFavorite()) {
//            收藏
            return userSolutionFavoriteMapper.insert(userSolutionFavorites);
        } else {
//            取消收藏
            return userSolutionFavoriteMapper.delete(userSolutionFavorites);
        }
    }

    @Override
    public List<SolutionAppResDTO> selectSolutionsVOList(Long userId) {
        List<SolutionAppResDTO> list = userSolutionFavoriteMapper.selectSolutionsVOList(userId);
        list.forEach(item -> {
            item.setCategories(categoryMapper.selectCategoriesBySolutionId(item.getId()));
        });
        return list;
    }
}
